package com.chj.leetcode.tijie200to250;

import java.util.ArrayDeque;
import java.util.Deque;

public class Code232_ImplementQueueUsingStacks {
	// 栈实现队列
	public class TwoStacks_Queue {
		Deque<Integer> stackPush = new ArrayDeque<>();
		Deque<Integer> stackPop = new ArrayDeque<>();

		public void push(int p) {
			stackPush.push(p);
		}

		public int pop() {
//			if (stackPush.isEmpty() && stackPop.isEmpty()) {
//				throw new RuntimeException("Queue is empty!");
//			} else if (stackPop.isEmpty()) {
//				while (!stackPush.isEmpty()) {
//					stackPop.push(stackPush.pop());
//				}
//			}
			pp();
			return stackPop.pop();
		}

		public int peek() {
//			if (stackPush.isEmpty() && stackPop.isEmpty()) {
//				throw new RuntimeException("Queue is empty!");
//			} else if (stackPop.isEmpty()) {
//				while (!stackPush.isEmpty()) {
//					stackPop.push(stackPush.pop());
//				}
//			}
			pp();
			return stackPop.peek();
		}

		/** Returns whether the queue is empty. */
		public boolean empty() {
			return stackPush.isEmpty() && stackPop.isEmpty();
		}

		private int pp() {
			if (stackPush.isEmpty() && stackPop.isEmpty()) {
				return -1;
			} else if (stackPop.isEmpty()) {
				while (!stackPush.isEmpty()) {
					stackPop.push(stackPush.pop());
				}
			}
			return 1;
		}

	}
}
